from selenium import webdriver
import time
import random
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions

# 获取驱动
driver = webdriver.Chrome()
# 通过驱动打开网页
driver.get("http://127.0.0.1/ranzhi/www")
driver.maximize_window()
time.sleep(1)

# 登录
driver.find_element("id","account").send_keys("admin")
driver.find_element("id","password").send_keys("123456")
driver.find_element("id","submit").click()
time.sleep(1)

#点击后台管理
a=driver.find_element("id","s-menu-superadmin")
a.click()
time.sleep(1)
#切换进iFrame
i = driver.find_element("id","iframe-superadmin")
driver.switch_to.frame(i)
time.sleep(1)


b=driver.find_element("link text","添加成员").click()
#print(b.text)

locator1=("id","submit")#出现保存
e = WebDriverWait(driver,20,0.5).until(expected_conditions.presence_of_element_located(locator=locator1))
# driver.find_element("class name","shortcut.user").click()
time.sleep(3)

driver.find_element("id","account").send_keys(f"alex91sad999")
time.sleep(2)
driver.find_element("id","realname").send_keys("as3d")
time.sleep(2)
driver.find_element("id","genderm").click()
time.sleep(1)

se = driver.find_element("id","role")
select1 = Select(se)  # 强转为Select对象

roles = select1.options # 获取所有的选项对象
list1=[]
for role in roles:
    list1.append(role.text)
    print(role.text,"----下拉框选项内容")
select1.select_by_visible_text(list1[2])
time.sleep(1)
driver.find_element("id","password1").send_keys("123456")
time.sleep(1)
driver.find_element("id","password2").send_keys("123456")
time.sleep(1)
driver.find_element("id","email").send_keys(f"1234asd56@123.com")
time.sleep(1)
driver.find_element("id","submit").click()
time.sleep(5)
locator2=("class name","panel-heading")#出现保存
e = WebDriverWait(driver,20,0.5).until(expected_conditions.presence_of_element_located(locator=locator2))
# 获取当前时间
datestr1 = time.strftime("%Y%m%d%H%M%S",time.localtime())
print(datestr1)
# 截屏
driver.get_screenshot_as_file(f"d:\\baidu_{datestr1}.png")

# 切换到上一层
# driver.switch_to.parent_frame()
# 切换到最外层
driver.switch_to.default_content()